//------------------------------------------------------------------------------
#include <QtGui>
#include <QSqlRecord>
#include "CEcheancierFrm.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
CEcheancierFrm::CEcheancierFrm(QWidget *parent) : QDialog(parent) {
	setupUi(this);
	
	createGrille();
	loadQui();
}
//------------------------------------------------------------------------------
void CEcheancierFrm::createGrille(void) {
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelString(cneClient, "client", tr("Client"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cneNum, "numero", tr("Num"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cneNom, "nom", tr("Nom"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cneAction, "description", tr("Action"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDate(cneDate, "dateEcheance", tr("Echéance"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cneQui, "destinataire", tr("Par"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cneCommentaires, "commentaires", tr("Commentaires"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	
	tableActions->setColumnsModel(columnModel);
	
	tableActions->setWordWrap(true);
	tableActions->setColumnWidth(cneClient, 80);
	tableActions->setColumnWidth(cneNum, 50);
	tableActions->setColumnWidth(cneAction, 175);
	tableActions->setColumnWidth(cneDate, 80);
	tableActions->setColumnWidth(cneQui, 80);
	tableActions->setColumnWidth(cneNom, 250);
	tableActions->setColumnWidth(cneCommentaires, 420);
}
//------------------------------------------------------------------------------
void CEcheancierFrm::loadGrille(int verticalScrollBarPosition) {
	QString requete, cond;
	QSqlQuery *query;
	
	if(cbQui->itemData(cbQui->currentIndex()).toString() != QString("")) {
		cond="WHERE destinataire=\""+cbQui->currentText()+"\" ";
	}
	
	requete="SELECT client, numero, nom, description, dateEcheance, destinataire, ec.commentaires, ";
	requete+="dateEcheance <= CURDATE() AS echeancePasse, DATE_ADD(dateClique, INTERVAL 5 DAY) > CURDATE() AS onLAfficheEncore ";
	requete+="FROM echeancesClient ec INNER JOIN client ON client=code ";
	requete+=cond;
	requete+="ORDER BY dateEcheance";
	
	clearTable();
	tableActions->setSortingEnabled(false);
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		tableActions->loadGrille(query);
	}
	
	delete query;
	
	tableActions->verticalScrollBar()->setValue(verticalScrollBarPosition);
	tableActions->setSortingEnabled(true);
	tableActions->sortByColumn(cneDate, Qt::AscendingOrder);
}
//------------------------------------------------------------------------------
void CEcheancierFrm::loadQui(void) {
	QString requete;
	QSqlQuery *query;
	int i, sel=0;
	
	cbQui->addItem("Tout le monde", QVariant(""));
	
	requete="SELECT code FROM utilisateur ORDER BY code ";
	if(CMySQL::getInstance()->execQuery(requete, &query)) {
		i=1;
		while(query->next()) {
			QSqlRecord rec=query->record();
			QString code=rec.value("code").toString();
			
			cbQui->addItem(code, QVariant(code));
			if(code == CMySQL::getInstance()->getUser())
			{
				sel=i;
			}
			i++;
		}
	}
	delete query;
	
	cbQui->setCurrentIndex(sel);
}
//------------------------------------------------------------------------------
void CEcheancierFrm::clearTable(void) {
	int X, Y;
	for(Y=0;Y<tableActions->rowCount();Y++) {
		for(X=0;X<tableActions->getColumnModelCount();X++) {
			delete tableActions->item(Y, X);
		}
	}
	tableActions->setRowCount(0);
}
//------------------------------------------------------------------------------
void CEcheancierFrm::on_tableActions_cellDoubleClicked(int row, int col) {
	QString client;
	int numero;
	QString requete;
	QDate dateEcheance;
	int oldVerticalScrollBarPosition=tableActions->verticalScrollBar()->value();

	client=tableActions->item(row, cneClient)->text();
	numero=tableActions->item(row, cneNum)->text().toInt();
	dateEcheance=QDate::fromString(tableActions->item(row, cneDate)->text(), "dd/MM/yyyy");

	if(dateEcheance <= CMySQL::getInstance()->getCurrentDate()) {
		requete="UPDATE echeancesClient SET ";
		requete+="dateEcheance=DATE_ADD(dateEcheance, INTERVAL repete MONTH), ";
		requete+="dateClique=CURDATE() ";
		requete+="WHERE client=\""+client+"\" AND numero="+QString::number(numero)+" ";
	}else {
		requete="UPDATE echeancesClient SET ";
		requete+="dateEcheance=IF(DATE_ADD(dateClique, INTERVAL 5 DAY) > CURDATE(), DATE_SUB(dateEcheance, INTERVAL repete MONTH), DATE_ADD(dateEcheance, INTERVAL repete MONTH)), ";
		requete+="dateClique=IF(DATE_ADD(dateClique, INTERVAL 5 DAY) > CURDATE(), \"0000-00-00\", CURDATE()) ";
		requete+="WHERE client=\""+client+"\" AND numero="+QString::number(numero)+" ";
	}

	CMySQL::getInstance()->execQuery(requete);

	loadGrille(oldVerticalScrollBarPosition);
}
//------------------------------------------------------------------------------
void CEcheancierFrm::on_cbQui_currentIndexChanged(const QString &text) {
	loadGrille();
}
//------------------------------------------------------------------------------
void CEcheancierFrm::on_tableActions_beforeChangeRecord(const QSqlQuery *query, int row) {
	QFont f;
	QSqlRecord rec=query->record();
	QBrush brushF=QBrush(rec.value("echeancePasse").toString() == "1" ? QColor(255, 255, 255) : (rec.value("onLAfficheEncore").toString() == "1" ? QColor (255, 255, 255) : QColor(0, 0, 0)));
	QBrush brushB=QBrush(rec.value("echeancePasse").toString() == "1" ? QColor(255, 0, 0) : (rec.value("onLAfficheEncore").toString() == "1" ? QColor (0, 128, 0) : QColor(255, 255, 255)));
	int X;
	
	for(X=0;X<tableActions->getColumnModelCount();X++) {
		QTableWidgetItem *item=tableActions->item(row, X);
		
		item->setForeground(brushF);
		item->setBackground(brushB);
		f=item->font();
		f.setWeight(rec.value("echeancePasse").toString() == "1" ? QFont::Bold : (rec.value("onLAfficheEncore").toString() == "1" ? QFont::Bold : QFont::Normal));
		item->setFont(f);
	}
}
//------------------------------------------------------------------------------